function VarBeta = VarBetaNI(d)

   global priorvar Cbar1 Cbar0 Vtilde1 Vtilde0 Sigma1 Sigma0
   global xbarNI XNI   
    
      
   Ky1=Vtilde1(d==1, d==1) + diag(Sigma1(d==1));
   Ky0=Vtilde0(d==0, d==0) + diag(Sigma0(d==0));
       
   X1=XNI(logical(d),:);
   X0=XNI(logical(1-d),:);
       
       
    VarBHatPart1 = Cbar1(d==1) * (Ky1 \ Cbar1(d==1)') ...
                 + Cbar0(d==0) * (Ky0 \ Cbar0(d==0)');       
    
    Ky1BSX= Ky1 \ X1;
    Ky0BSX= Ky0 \ X0;
   
 
    Correction =  ((xbarNI - Cbar1(d==1) * Ky1BSX) / (X1' * Ky1BSX)) * (xbarNI - Cbar1(d==1) * Ky1BSX)' ...
                + ((xbarNI - Cbar0(d==0) * Ky0BSX) / (X0' * Ky0BSX)) * (xbarNI - Cbar0(d==0) * Ky0BSX)';

    VarBeta = priorvar - VarBHatPart1 + Correction;
return
